
//引入http模块
var http=require('http');

//fs模块

var fs=require('fs');

//path模块
var path=require('path');  /*nodejs自带的模块*/

var mimeModel=require('./model/getmimefromfile');

http.createServer((req,res)=>{
    var pathname=req.url;
	if(pathname=='/'){
		pathname='/index.html'; /*默认加载的首页*/
	}
    console.log('url为:'+pathname);
    
	//获取文件的后缀名
    var extname=path.extname(pathname);
    console.log('url中文件的后缀:'+extname);
    

    if(pathname!='/favicon.ico'){  /*过滤请求favicon.ico*/
        fs.readFile('./05/static/'+pathname,(err,data)=>{
            if(err){
                console.log('404');
                fs.readFile('./05/static/404.html',(err404,data404)=>{
                    if(err404){
						console.log(err404);
					}
					res.writeHead(404,{"Content-Type":"text/html;charset=utf-8"});
					res.write(data404);
					res.end(); /*结束响应*/
                });
            }else{
                var mime=mimeModel.getMime(fs,extname);  /*获取文件类型*/
                res.writeHead(200,{"Content":`${mime};charset=utf-8`});
                res.write(data);
                res.end();
            }
        })        
    }

}).listen(8001);